/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.maincouponsend.rest;

import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.maincouponsend.domain.TMainCouponSend;
import com.xunyin.modules.maincouponsend.service.TMainCouponSendService;
import com.xunyin.modules.maincouponsend.service.dto.TMainCouponSendDto;
import com.xunyin.modules.maincouponsend.service.dto.TMainCouponSendQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-28
 */
@AllArgsConstructor
@Api(tags = "共享空间：优惠券发送管理")
@RestController
@RequestMapping("api")
public class TMainCouponSendController {

    private final TMainCouponSendService tMainCouponSendService;
    private final IGenerator generator;

    @Log("查询共享空间：优惠券发送")
    @ApiOperation("查询共享空间：优惠券发送")
    @GetMapping(value = "/tMainCouponSend")
    @PreAuthorize("hasAnyRole('admin','MAINPART_ALL','MAINPART_SELECT')")
    public ResponseEntity getTMainCouponSends(TMainCouponSendQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(tMainCouponSendService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增共享空间：优惠券发送")
    @ApiOperation("新增共享空间：优惠券发送")
    @PostMapping(value = "/tMainCouponSend")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody TMainCouponSend resources) {
        return new ResponseEntity<>(tMainCouponSendService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改共享空间：优惠券发送")
    @ApiOperation("修改共享空间：优惠券发送")
    @PutMapping(value = "/tMainCouponSend")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody TMainCouponSend resources) {
        tMainCouponSendService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("共享空间：修改优惠券活动状态")
    @ApiOperation("共享空间：修改优惠券活动状态")
    @PostMapping(value = "/updateCouponSendStatus")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity<Object> updateCouponSendStatus(@Validated @RequestBody TMainCouponSend resources) {
        //tMainCouponSendService.updateById(resources);
        tMainCouponSendService.updateCouponSendStatus(resources.getId(), resources.getCouponStatus());
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("共享空间：添加用户优惠券")
    @ApiOperation("共享空间：添加用户优惠券")
    @PostMapping(value = "/sendCoupon")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity<Object> sendCoupon(@Validated @RequestBody TMainCouponSendDto resources) {

        tMainCouponSendService.sendCoupon(resources);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("删除共享空间：优惠券发送")
    @ApiOperation("删除共享空间：优惠券发送")
    @DeleteMapping(value = "/tMainCouponSend/{id}")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        tMainCouponSendService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
